package cc.linker.steplink.crm.merchant.service;


import cc.linker.steplink.crm.merchant.dao.GiftTaskDao;
import cc.linker.steplink.crm.response.GlobalException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class GiftTaskService {

    @Autowired
    private GiftTaskDao giftTaskDao;


    public List<Map<String, Object>> getList(Map<String, Object> map) {
        List<Map<String, Object>> list = giftTaskDao.getList(map);
        for(Map<String, Object> da:list){
            if(!StringUtils.isEmpty(map.get("startTime"))){
                da.put("startTime",map.get("startTime"));
            }
            if(!StringUtils.isEmpty(map.get("endTime"))){
                da.put("endTime",map.get("endTime"));
            }
            Integer sendNum = giftTaskDao.getSendNum(da);
            Integer giftNum = giftTaskDao.getGiftNum(da);
            da.put("sendNum",sendNum==null?0:sendNum);
            da.put("giftNum",giftNum==null?0:giftNum);
        }
        return list;
    }

    public void save(Map<String, Object> param) {
        if(!StringUtils.isEmpty(param.get("name"))){
            //重名判断
            int count = giftTaskDao.getRename(param);
            if(count>0){
                throw GlobalException.build("该礼包名称已存在");
            }
        }
        if(!StringUtils.isEmpty(param.get("id"))){
            //编辑
            giftTaskDao.edit(param);
        }else{
            //新增
            giftTaskDao.add(param);
        }
    }

    public void del(Map<String, Object> param) {
        giftTaskDao.del(param);
    }

    public Map<String, Object> getInfo(Map<String, Object> param) {
        return giftTaskDao.getInfo(param);
    }

    public List<Map<String, Object>> getSendList(Map<String, Object> param) {
        return giftTaskDao.getSendList(param);
    }

    public List<Map<String, Object>> getAllTask() {
        return giftTaskDao.getAllTask();
    }

    public List<Map<String, Object>> getBirthCustomerList(String today) {
        return giftTaskDao.getBirthCustomerList(today);
    }

    public void addGiftLog(Map<String, Object> giftLog) {
        giftTaskDao.addGiftLog(giftLog);
    }

    public Integer getSendLogCount(Map<String, Object> giftLog) {
        return giftTaskDao.getSendLogCount(giftLog);
    }
}
